package com.yuandian.business.monitor.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.business_monitor.entity.E2eStudyResult;
import com.yuandian.api.business_monitor.po.E2eStudyParam;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.mapper.E2eStudyResultMapper;
import com.yuandian.business.monitor.service.IE2eStudyResultService;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 说明：端到端学习结果service实现
 * @Author zfsn
 * @Date 2023/7/12 14:30
 */
@Service
public class E2eStudyResultServiceImpl extends ServiceImpl<E2eStudyResultMapper, E2eStudyResult>
        implements IE2eStudyResultService {

    /**
     * 分页查询端到端学习结果列表
     * @param param 端到端学习请求参数
     * @return 通用返回结果
     */
    @Override
    public R page(E2eStudyParam param) {
        Page<E2eStudyResult> page = new Page<>(param.getPageNum(), param.getPageSize());
        if (StringUtils.isEmpty(param.getOrderByColumn())) {
            page.addOrder(OrderItem.desc("task_id"));
        } else if ("asc".equals(param.getIsAsc())) {
            page.addOrder(OrderItem.asc(StringUtils.toUnderScoreCase(param.getOrderByColumn())));
        } else if ("desc".equals(param.getIsAsc())) {
            page.addOrder(OrderItem.desc(StringUtils.toUnderScoreCase(param.getOrderByColumn())));
        }
        Page<E2eStudyResult> pages = lambdaQuery()
                .eq(param.getTaskId() != null, E2eStudyResult::getTaskId, param.getTaskId())
                .page(page);
        return R.ok(new PageDataInfo(page, pages.getRecords()));
    }
}
